Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
print("Hello, World!")
Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
print("Hello, World!")
In Python, variables are created when you assign a value to it:
x = 5
y = "Hello, World!"
Python has commenting capability for the purpose of in-code documentation. Comments start with a #, and Python will render the rest of the line as a comment:
#This is a comment.
print("Hello, World!")
In programming, data type is an important concept. Variables can store data of different types, and different types can do different things. Python has the following data types built-in by default, in these categories:
x = 5
print(type(x))
Used to get the data type
Python supports the usual logical conditions from mathematics:
An "if statement" is written by using the if keyword.
aa = 33
b = 200
if b > a:
print("b is greater than a")
Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose.
a = 33
b = 200
if b > a:
print("b is greater than a") # you will get an error
The elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition".
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages. With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
With the break statement we can stop the loop before it has looped through all the items:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
A function is a block of code which only runs when it is called You can pass data, known as parameters, into a function. A function can return data as a result.
In Python a function is defined using the def keyword:
def my_function():
print("Hello from a function")
To call a function, use the function name followed by parenthesis:
def my_function():
print("Hello from a function")
my_function())
File handling is an important part of any web application. Python has several functions for creating, reading, updating, and deleting files.
f = open("demofile.txt")
To open a file for reading it is enough to specify the name of the file: